home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Workspace / Briefcase / Source / BriefDoc.m < prev    next >
Text File  |  1992-08-10  |  6KB  |  227 lines

  1. #import "BriefDoc.h"
  2. #import "Briefcase.h"
  3. #import <appkit/appkit.h>
  4. #import "Global.h"
  5.  
  6. @implementation BriefDoc: MultDoc
  7.  
  8. /* Canon Information Systems is not responsible for anything anyone does with this   */
  9. /* code, nor are they responsible for the correctness of this code.  Basically, this */
  10. /* has very little to do with the company I work for, and you can't blame them.      */
  11.  
  12. /* This file is best read in a window as wide as this comment, and with tab settings */
  13. /* of 4 spaces and indent setting of 4 spaces (at least, that's what I use).         */
  14.  
  15. /* You are welcome to do as you would with this file under the following conditions. */
  16. /* First, I accept no blame for anything that goes wrong no matter how you use it,   */
  17. /* no matter how catastrophic, not even if it stems from a bug in my code.             */
  18. /* Second, please keep my notices on it when/if you distribute it.                     */
  19. /* Third, if you discover any bugs or have any comments, PLEASE TELL ME!  Code won't */
  20. /* get better without people picking it apart and giving the writer feedback.        */
  21. /* Fourth, if you modify it, please keep a notice that your version is based on mine */
  22. /* in the source files (and keep the notice that mine is based on four other pieces  */
  23. /* of code :<).  Thanks, and have fun.  - Subrata Sircar, ssircar@canon.com             */
  24.  
  25. /* Version 0.9b        Apr-19-92        First Public Release    */
  26. /* Version 0.95        Apr-29-92        Bug Fixes                */
  27. /* Version 1.0b        Aug-08-92        Minor Bug Fixes            */
  28.  
  29. /* New creation methods */
  30.  
  31. + initialize
  32. /* Class variable initialization.  DO NOT call from subclasses. */
  33. {
  34.     if (self == [BriefDoc class]) {
  35.         [self setVersion:100];
  36.         [self setExtension:LocalString("bc")];
  37.         [self setDefault:LocalString("Untitled%d")];
  38.     }
  39.     return self;
  40. }
  41.  
  42. + newFromFile:(const char *)file
  43. {
  44.     NXStream *stream;
  45.     id text;
  46.     int count;
  47.     char tempBuf[8192], fileBuf[16384], *tempPtr, *lastPtr;
  48.     
  49.     stream = NXMapFile(file,NX_READONLY);
  50.     if (stream) {
  51.         self = [[self class] new];
  52.         text = [view docView];
  53.         [window disableDisplay];
  54.         [text readText:stream];
  55.         [text selectAll:self];
  56.         count = [text textLength];
  57.         [text getSubstring:tempBuf start:0 length:count];
  58.         tempPtr = lastPtr = tempBuf;
  59.         *fileBuf = '\0';
  60.         while (tempPtr = index(tempPtr,'/')) {
  61.             if (*(++tempPtr) == '*') {
  62.                 lastPtr = ++tempPtr;
  63.                 tempPtr = index(tempPtr,'*');
  64.                 count = (int)(tempPtr-lastPtr);
  65.                 strncat(fileBuf,lastPtr,count);
  66.                 strcat(fileBuf,"\n");
  67.                 tempPtr += 2;
  68.             }
  69.         }
  70.         [text selectAll:self];
  71.         [text replaceSel:fileBuf];
  72.         [text sizeToFit];
  73.         [self setName:file];
  74.         NXCloseMemory(stream,NX_FREEBUFFER);
  75.         [self setSavedDocument:YES];
  76.         [self setEmpty:NO];
  77.         [window reenableDisplay];
  78.         [window display];
  79.         [window makeKeyAndOrderFront:self];
  80.         return self;
  81.     } else {
  82.         Notify(LocalString("BriefDoc: can't open file"),file);
  83.         return nil;
  84.     }
  85. }
  86.  
  87. - setUpNib
  88. {
  89.     LoadLocalNib(LocalString("BriefDoc.nib"),self,NO,MyZone);
  90.     [[view docView] setDelegate:self];
  91.     [window makeFirstResponder:[view docView]];
  92.     return self;
  93. }
  94.  
  95. - addFiles:(char *)fileList
  96. {
  97.     int length = 0;
  98.     
  99.     id text = [view docView];
  100.     length = [text textLength];
  101.     [text setSel:length :length];
  102.     [text replaceSel:fileList];
  103.     [[text window] display];
  104.     /* hack to keep status correct - text object doesn't tell me when this happens */
  105.     if (fileList && *fileList) {
  106.         [self dirty:YES];
  107.         [self setEmpty:NO];
  108.     }
  109.     return self;
  110. }
  111.  
  112. /* Methods related to naming/saving this document. */
  113.  
  114. - (const char *)_cname
  115. /*
  116.  * Returns a fully specified file name with ".c" extension.
  117.  */
  118. {
  119.     char *ext;
  120.     static char textnamebuf[MAXPATHLEN+1];
  121.  
  122.     sprintf(textnamebuf,[self fileName]);
  123.     ext = rindex(textnamebuf,'.');
  124.     *(ext++) = '.';
  125.     *(ext++) = 'c';
  126.     *ext = '\0'; 
  127.     return textnamebuf;
  128. }
  129.  
  130. - (const char *)_execname
  131. /*
  132.  * Returns a fully specified file name with no extension.
  133.  */
  134. {
  135.     char *ext;
  136.     static char execnamebuf[MAXPATHLEN+1];
  137.  
  138.     sprintf(execnamebuf,[self fileName]);
  139.     ext = rindex(execnamebuf,'.');
  140.     *ext = '\0'; 
  141.     return execnamebuf;
  142. }
  143.  
  144. - save
  145. {    
  146.     NXStream *ts = NULL;
  147.     int fd = 0;
  148.     
  149.     fd = open([self fileName], O_CREAT | O_WRONLY | O_TRUNC, 0644);
  150.     ts = NXOpenFile(fd, NX_WRITEONLY);
  151.     if (ts) {
  152.         id text = [view docView];
  153.         int count = [text textLength];
  154.         char tempBuf[count + 8192];
  155.         char fileBuf[count + 8192];
  156.         char quoteBuf[3];
  157.         char *tempPtr, *lastPtr;
  158.         const char *fn = [self fileName], *en = [self _execname], *cn = [self _cname];
  159.         char quote = '"';
  160.  
  161.         sprintf(quoteBuf,"%c",quote);
  162.         [window disableDisplay];
  163.         [text selectAll:self];
  164.         [text getSubstring:tempBuf start:0 length:count];
  165.         tempPtr = lastPtr = tempBuf;
  166.         *fileBuf = '\0';
  167.         while (tempPtr = index(tempPtr,'\n')) {
  168.             count = (int)(tempPtr-lastPtr);
  169.             strcat(fileBuf,"/*");
  170.             strncat(fileBuf,lastPtr,count);
  171.             strcat(fileBuf,"*/\n");
  172.             tempPtr++;
  173.             lastPtr=tempPtr;
  174.         }
  175.         tempPtr = lastPtr = tempBuf;
  176.         strcat(fileBuf,"#include <stdlib.h>\n\n");
  177.         strcat(fileBuf,"main()\n{\nsystem(");
  178.         strcat(fileBuf,quoteBuf);
  179.         strcat(fileBuf,"open ");
  180.         while (tempPtr = index(tempPtr,'\n')) {
  181.             count = (int)(tempPtr-lastPtr);
  182.             strncat(fileBuf,lastPtr,count);
  183.             strcat(fileBuf," ");
  184.             tempPtr++;
  185.             lastPtr=tempPtr;
  186.         }
  187.         tempPtr = rindex(fileBuf,' ');
  188.         *tempPtr = '\0';
  189.         strcat(fileBuf,quoteBuf);
  190.         strcat(fileBuf,");\n}\n");
  191.         [text replaceSel:fileBuf];
  192.         [text writeText:ts];
  193.         NXFlush(ts);
  194.         NXClose(ts);
  195.         [text selectAll:self];
  196.         [text replaceSel:tempBuf];
  197.         *tempBuf = '\0';
  198.         sprintf(tempBuf,"mv %s %s",fn,cn);
  199.         system(tempBuf);
  200.         *tempBuf = '\0';
  201.         sprintf(tempBuf,"cc -O -o %s %s",en,cn);
  202.         system(tempBuf);
  203.         *tempBuf = '\0';
  204.         sprintf(tempBuf,"strip %s",en);
  205.         system(tempBuf);
  206.         *tempBuf = '\0';
  207.         sprintf(tempBuf,"mv %s %s",cn,fn);
  208.         system(tempBuf);
  209.         [window reenableDisplay];
  210.         [self dirty:NO];
  211.         [self setSavedDocument:YES];
  212.     } else Notify(LocalString("BriefDoc: save - Unable to create file "),[self fileName]);
  213.     close(fd);
  214.     return self;
  215. }
  216.  
  217. /* Text Delegate methods */
  218.  
  219. - textDidGetKeys:sender isEmpty:(BOOL)flag
  220. {
  221.     [self dirty:YES];
  222.     [self setEmpty:flag];
  223.     return self;
  224. }
  225.  
  226. @end
  227.